var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var listdata = mm("do_ListData");
var circleUrl = require("url/circle");
var httpSilence = require("helper/httpSilence");
var helperOpen = require("helper/open");
var gridCircle = ui("gridCircle");
gridCircle.bindItems(listdata);

var myPrivateCircles = circleUrl.url.member.myPrivateCircles;//我设置了隐私的圈子
var addPrivateUrl = circleUrl.url.member.addPrivateBath;
var delPrivateUrl = circleUrl.url.member.delPrivate;

/*********************加载头部--begin***********************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "隐藏我的圈子",
})
header.refreshData();
//左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage()
});
//右上角-完成
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "完成";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	do_App.closePage()
});
/*********************加载头部--end***********************/

//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	loadPage();
});

//点击图片-删除隐私
do_Page.on("image-touched",function(circleId){
	if(circleId=="add"){//选择圈子
		helperOpen.open("source://view/user/privacy/circleSet/circleSelect.ui",{});
	}else{
		nf.confirm("确定要取消隐藏这个圈子吗?", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				httpSilence.post(delPrivateUrl,{"circleId":circleId},function(data){
					loadPage();
				});
			}
		})
	}
	
});

function loadPage() {
	listdata.removeAll();
	var addBtnData = {name:"",id:"add",fullLogo:"source://image/common/add2.png",showDelBtn:false};
	listdata.addOne(addBtnData,0);//固定的一个(用于添加)
	gridCircle.refreshItems();
	httpSilence.post(myPrivateCircles,{},function(data){
		if(data.circleList){
			listdata.addData(data.circleList,0);
			gridCircle.refreshItems();
		}
	});
}

do_Page.on("result", function(circleList){
	var circleIdList = [];
	for(var i=0;i<circleList.length;i++){
		if(circleList[i]!=null){
			circleIdList.push(circleList[i].id);
		}
	}
	if(circleIdList.length <=0){
		return;
	}
	var circleIdsString = circleIdList.join(';');//圈子ID列表字符串
	httpSilence.post(addPrivateUrl,{"circleIds":circleIdsString},function(data){
		listdata.addData(circleList,0);//在前面插入
		gridCircle.refreshItems();
	});
	
});